<template>
  <el-dialog v-model="dialogVisible" width="60%" :title="`${drawerProps.title}角色`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="用户账号" prop="account">
        <el-input
          v-model.trim="drawerProps.row.account"
          :disabled="undefined != drawerProps.userId && drawerProps.userId > 0"
          clearable
        ></el-input>
      </el-form-item>

      <el-form-item label="用户昵称" prop="nickName">
        <el-input v-model.trim="drawerProps.row.nickName" clearable></el-input>
      </el-form-item>

      <el-form-item label="邮箱" prop="email">
        <el-input v-model.trim="drawerProps.row.email" clearable></el-input>
      </el-form-item>

      <el-form-item label="手机号码" prop="phoneNumber">
        <el-input v-model.trim="drawerProps.row.phoneNumber" clearable></el-input>
      </el-form-item>

      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="drawerProps.row.sex">
          <el-radio :label="`0`">保密</el-radio>
          <el-radio :label="`1`">男</el-radio>
          <el-radio :label="`2`">女</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="头像" prop="avatar">
        <UploadImg v-model:image-url="drawerProps.row.avatar" width="135px" height="135px" :file-size="3" :api="uploadFileApi">
          <template #empty>
            <el-icon><Avatar /></el-icon>
            <span>请上传头像</span>
          </template>
          <template #tip> 头像大小不能超过 3M </template>
        </UploadImg>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input v-model.trim="drawerProps.row.password" type="password" autocomplete="off" clearable></el-input>
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="drawerProps.row.status">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" v-model="drawerProps.row.remark"></el-input>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" v-show="!drawerProps.isView" @click="handleSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="RoleDrawer">
import md5 from "js-md5";
import { ref, reactive } from "vue";
import UploadImg from "@/components/Upload/Img.vue";
import { ElMessage, FormInstance } from "element-plus";
import { uploadFileApi } from "@/api/modules/upload";
import { SystemUser } from "@/api/interface/systemUser";
import { getUserDetailApi } from "@/api/modules/systemUser";

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<SystemUser.UserDetailRes>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
  menuId?: number;
  parentId?: number;
  roleId?: number;
  userId?: number;
}

const dialogVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

const rules = reactive({
  account: [{ required: true, message: "请填写用户账号" }],
  nickName: [{ required: true, message: "请填写用户昵称" }],
  status: [{ required: true, message: "请选择用户状态" }],
  password: [{ required: true, message: "请输入密码" }]
});

// 获取角色详情
const getUserDetail = async (userId: number) => {
  const { data } = await getUserDetailApi(userId);
  drawerProps.value.row = data;
};

// 接收父组件传过来的参数
const acceptParams = async (params: DrawerProps) => {
  drawerProps.value = params;

  drawerProps.value.row.status = 1;
  drawerProps.value.row.sex = "0";

  // 获取用户详情
  if (undefined != params.userId && params.userId > 0) {
    drawerProps.value.userId = params.userId;
    await getUserDetail(params.userId);
    rules.password = [{ required: false, message: "请输入密码" }];
  }

  dialogVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      if (undefined != drawerProps.value.row.password && drawerProps.value.row.password.length > 0) {
        drawerProps.value.row.password = md5(drawerProps.value.row.password);
      }
      await drawerProps.value.api!(drawerProps.value.row);
      ElMessage.success({ message: `${drawerProps.value.title}用户成功！` });
      drawerProps.value.getTableList!();
      dialogVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
<style scoped>
.el-icon-inline-block {
  display: inline-block;
}
</style>
